package cn.bonoon.kernel.web.html.form;

import static cn.bonoon.kernel.util.StringHelper.isNotEmpty;
import cn.bonoon.kernel.annotations.ResetProperty;
import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.annotations.form.PropertyHelper;
import cn.bonoon.kernel.web.html.FormSetting;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;

/**
 * 自动生成，表单的属性项
 * @author jackson
 *
 */
public abstract class AbstractDetailablePropertyParser<F extends FormSetting> extends AbstractPropertyParser<F>{

	protected String unit;
	protected final UIPropertyAccessor accessor;
	private StringBuilder helperHtml = null;
	
	protected AbstractDetailablePropertyParser(F formParser, UIPropertyAccessor accessor) {
		super(formParser);
		this.accessor = accessor;
	}
	
	public UIPropertyAccessor getAccessor(){
		return accessor;
	}

	@Override
	protected final StringBuilder buildHead(StringBuilder html) {
		super.buildHead(html);
		if (isNotEmpty(unit)) html.append('(').append(unit).append(')');
		return html;
	}

	protected void init() {
		int width = formParser.getPropertyWidth(), headWidth = formParser.getHeadWidth();
		if(formParser.getColumn() < colspan){
			colspan = formParser.getColumn();//如果定义只有2列，那么就不允许跨3列的情况出现
		}
		if (colspan > 0) {
			// 横夸列
			realWidth = width + colspan * (width + headWidth);
		} else {
			realWidth = width;
		}
		ResetProperty rename = accessor.getPropertyReseter();
		if(null != rename){
			if(StringHelper.isEmpty(name)){
				name = rename.name();
			}
			if(StringHelper.isEmpty(unit)){
				unit = rename.unit();
			}
		}
		if(StringHelper.isEmpty(name)){
			name = accessor.getName();
		}
	}

	@Override
	protected void buildContented(UIPropertyBuilder pb) {
		if (null != helperHtml){
			pb.getInitHtml().append(helperHtml);
		}
	}

	protected void addHelper(UIPropertyAccessor accessor) {// 显示帮助信息
		PropertyHelper helper = accessor.getAnnotation(PropertyHelper.class);
		if (null == helper) return;
		helperHtml = new StringBuilder();
		String v = helper.value();
		switch (helper.type()) {
		case DIRECT:
			helperHtml.append("<span class='form-helper' title='").append(helper.name()).append("'>[").append(v).append(']');
			calculateHelperWidth(v.length());
			helperHtml.append("</span>");
			break;
		case WRAP:
			helperHtml.append("<div class='form-helper' title='").append(helper.name()).append("'>").append(v);
			//这种情况下不需要计算文本框的宽度
			//calculateHelperWidth(v.length());
			helperHtml.append("</div>");
			break;
//		case URL:
//			helperHtml.append("<a class='helper helper-tooltip-ajax' href=\"");
//			helperHtml.append("#springUrl('").append(v).append("')\">");
//			helperName(helper);
//			helperHtml.append("</a>");
//			break;
//		case LOAD:
//			helperHtml.append("<a class='helper helper-tooltip-div' href=\"");
//			helperHtml.append("javascript:void(null);\" title='#").append(v).append("'>");
//			helperName(helper);
//			helperHtml.append("</a>");
//			break;
		case PROMPT:
		default:
			helperHtml.append("<a class='form-helper form-helper-tooltip' href=\"");
			helperHtml.append("javascript:void(null);\" title='").append(v).append("'>");
			helperName(helper);
			helperHtml.append("</a>");
		}
	}

	/**
	 * 用于计算在输入框后面添加帮助说明时需要占用的空间位置
	 * @param length 占用的值
	 */
	protected void calculateHelperWidth(int length) { }

	private void helperName(PropertyHelper helper) {
		String n = helper.name(), i = helper.image();
		if (isNotEmpty(i)) {
			// 使用图片
			helperHtml.append("<img src=\"#springUrl('").append(i).append("')\" alt='").append(n).append("'/>");
			calculateHelperWidth(0);
		} else {
			helperHtml.append('[').append(n).append(']');
			calculateHelperWidth(n.length());
		}
	}
}
